package com.amat0urheu.controller.adminController;

import com.amat0urheu.dao.CloudMapper;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.RaceService;
import com.amat0urheu.service.cloudService.CloudService;
import com.amat0urheu.service.cloudService.VMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class VmServiceController {
    @Autowired
    CloudService cloudService;
    @Autowired
    VMService vmService;
    @Autowired
    RaceService raceService;

    @Autowired
    CloudMapper cloudMapper;
    @PreAuthorize("hasAuthority('ROLE_super')")
    @GetMapping("/cloud/getAllexamplesFromOpenstack")
    ReturnRes getVMservices() {
        return vmService.getVMservices();
    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/createVMservices")
    ReturnRes createVMservice(@RequestParam(name = "Name") String name,@RequestParam(name = "Num") Integer num,@RequestParam(name = "Time") Integer time) {
        return   vmService.createVMservices(name,num,time);
    }


    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/operateVMservice")
    ReturnRes operateVMservice(@RequestParam(name = "vmID")String vmID,@RequestParam(name = "action")String action) {

        return   vmService.stopVm(vmID,action);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/createRaceVm")
    ReturnRes submitCreateRaceVm(@RequestParam(name = "Id") Integer rId,@RequestParam(name = "Num") Integer num) {
        return   raceService.createRaceVm(rId,num);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/createClientVm")
    ReturnRes createClientVm(@RequestParam(name = "Id") Integer rId,@RequestParam(name = "Num") Integer num) {

        return    raceService.createClientVm(rId,num);
    }

    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/user/resetVmServer")
    ReturnRes resetVmServer(@RequestParam(name = "vmID") String vmID) {
        return   vmService.resetVmServer(vmID);
    }
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @PostMapping("/cloud/updatevmOfUser")
    ReturnRes updatevmOfUser(@RequestParam(name = "vmName") String vmName, Authentication authentication) {

        return   vmService.updatevmOfUser(vmName,authentication.getName());
    }

}